package com.practice.aiGenericsReflection009.generic;

import java.io.Serializable;

/**
 * 简单泛型方法
 */
public class GenericMethodSimple {
    public static <T> T getMiddle(T... a) {
        return a[a.length/2];
    }

    /**
     * 类型变量的限定
     * @param a
     * @param <T>
     * @return
     */
    public static <T extends Comparable> T min(T[] a) {
        if (a == null || a.length == 0) {
            return null;
        }
        T smallest = a[0];
        for (int i = 1; i < a.length; i++) {
            if (smallest.compareTo(a[i]) > 0) {
                smallest = a[i];
            }
        }
        return smallest;
    }

    public static <T extends Comparable & Serializable> T a(T a) {
        return null;
    }


    public static void main(String[] args) {
        GenericMethodSimple gms = new GenericMethodSimple();
        Integer middle = GenericMethodSimple.<Integer>getMiddle(1, 2, 3, 4);
        String middle1 = GenericMethodSimple.<String>getMiddle("a", "b", "c", "d");
        double middle2 = GenericMethodSimple.getMiddle(1.0, 1.1, 0.0);
        System.out.println(middle);
        System.out.println(middle1);
        Integer[] a = {1, 2, 3, 1, 7};
        Integer min = GenericMethodSimple.min(a);
        System.out.println(min);

    }
}
